package com.monkeyboy.rabbitmq.direct;

import com.monkeyboy.rabbitmq.RabbitmqConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * 作用： 生产者生产消息
 * 
 * @author Gavin
 * @time:2018年10月24日
 */
public class Producer {
	public static void main(String[] args) throws Exception {
		// 获取到连接一级mq通道(相当于mysql获取连接)
		Connection connection = RabbitmqConnectionUtil.getConnection();
		// 从连接中创建通道(相当于mysql获取连接后还得获取到statement才能操作数据库一样的道理)
		Channel channel = connection.createChannel(1);

		// 声明exchangeName
		String exchangeName = "test_direct_exchange";
		String exchangeType = "direct";
		String routingKey = "test.direct";// 路由key
		boolean durable = true;// 是否持久化
		// 声明交换机
		channel.exchangeDeclare(exchangeName, exchangeType, durable);
		// 发送消息到交换机中
		String msg = "生产者：交换机--》direct模式";
		channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());

		// 关闭通道和连接
		channel.close();
		connection.close();
	}
}
